#!/bin/bash

# P2任务演示启动脚本
# 容错降级、安全加固、灰度发布功能演示

echo "🚀 启动P2任务演示..."
echo "=================================="

# 检查Python环境
if ! command -v python3 &> /dev/null; then
    echo "❌ Python3 未安装"
    exit 1
fi

# 检查必要的Python包
echo "📦 检查依赖包..."
python3 -c "import asyncio, logging, time, json, hashlib, secrets, subprocess, sys, ssl, pathlib, shutil, difflib, statistics, collections, datetime, dataclasses, enum, typing, uuid" 2>/dev/null
if [ $? -ne 0 ]; then
    echo "❌ 缺少必要的Python包"
    exit 1
fi

# 检查cryptography包
python3 -c "import cryptography" 2>/dev/null
if [ $? -ne 0 ]; then
    echo "⚠️  警告: cryptography包未安装，mTLS功能可能不可用"
    echo "   安装命令: pip install cryptography"
fi

# 检查aiohttp包
python3 -c "import aiohttp" 2>/dev/null
if [ $? -ne 0 ]; then
    echo "⚠️  警告: aiohttp包未安装，HTTP客户端功能可能不可用"
    echo "   安装命令: pip install aiohttp"
fi

echo "✅ 依赖检查完成"

# 创建必要的目录
echo "📁 创建必要目录..."
mkdir -p hai-optimization/core/certs
mkdir -p hai-optimization/core/keys
mkdir -p hai-optimization/core/audit_logs
mkdir -p hai-optimization/core/versions
mkdir -p api-gateway

echo "✅ 目录创建完成"

# 设置环境变量
export PYTHONPATH="/home/ubuntu/PhotoEnhanceAI-web:$PYTHONPATH"

# 运行P2集成演示
echo "🎯 运行P2任务集成演示..."
echo "=================================="

cd /home/ubuntu/PhotoEnhanceAI-web/hai-optimization/core

python3 p2_integration_example.py

echo "=================================="
echo "🎉 P2任务演示完成！"
echo ""
echo "📋 演示内容包括："
echo "   ✅ 熔断器和降级策略"
echo "   ✅ 兜底机制"
echo "   ✅ 双向认证(mTLS)"
echo "   ✅ 动态密钥轮换"
echo "   ✅ 速率限制"
echo "   ✅ Feature Flag"
echo "   ✅ 配置变更审计"
echo "   ✅ 版本回滚机制"
echo "   ✅ 集成工作流"
echo ""
echo "📖 详细说明请查看: P2_TASKS_COMPLETED.md"
echo "🔧 配置文件位置: hai-optimization/core/"
echo "📊 日志文件位置: hai-optimization/core/audit_logs/"
